package com.xhu.water_supply.web.controller;

import com.xhu.water_supply.entity.Customer;
import com.xhu.water_supply.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 查找全部顾客
     * @param
     * @return
     */
    @RequestMapping(value="/findAll",method = RequestMethod.POST)
    public List<Customer> findAll(){
        System.out.println(customerService.findAll());
        return customerService.findAll();
    }

    /**
     * 查找顾客
     * @param cname 要查找的顾客对象
     * @return
     */
    @RequestMapping(value="/selectCustomer",method = RequestMethod.GET)
    public List<Customer> selectCustomer(String cname){
        return customerService.selectCustomer(cname);
    }

    /**
     * 查找修改顾客
     * @param cid
     * @return
     */
    @RequestMapping(value="getById",method = RequestMethod.GET)
    public Customer getById(String cid){
        System.out.println(customerService.getById(cid));
        return customerService.getById(cid);
    }

    /**
     * 添加顾客
     * @param customer 要添加的顾客对象
     * @return
     */
    @RequestMapping(value = "addCustomer",method = RequestMethod.POST)
    public Integer addCustomer(Customer customer){
        return customerService.addCustomer(customer);
    }

    /**
     * 修改顾客
     * @param customer
     * @return
     */
    @RequestMapping(value = "editCustomer",method = RequestMethod.POST)
    @ResponseBody
    public String editCustomer(Customer customer){
        int result = customerService.editCustomer(customer);
        if(result>=1) {
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

    /**
     * 删除顾客
     * @param cid 要删除的顾客id
     * @return
     */
    @RequestMapping(value = "/delCustomer",method = RequestMethod.POST)
    public Integer delCustomer(Integer cid){
        System.out.println(cid);
        int result=customerService.delCustomer(cid);
        return result;
    }
    /**
     * 查询页数
     * @param page
     * @return
     */
    @RequestMapping(value="/page",method = RequestMethod.POST)
    public List<Customer> page(Integer page){  //根据页码读取顾客
        int pageNow =page == null ? 1 :page;
        int pageSize=8;
        int startRows = pageSize * (pageNow - 1);
        List<Customer> list = customerService.queryPage(startRows);
        return list;
    }
    @RequestMapping(value="rows",method = RequestMethod.POST)
    public int rows(){
        return customerService.getRowCount();
    }  //总顾客数目
}